package com.beiming.moudle.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiming.common.utils.Result;
import com.beiming.common.utils.TokenUtil;
import com.beiming.moudle.auth.entity.BussEntity;
import com.beiming.moudle.auth.entity.BussUserEntity;
import com.beiming.moudle.auth.entity.ProductImageEntity;
import com.beiming.moudle.auth.entity.utillClass.BussPage;
import com.beiming.moudle.auth.entity.utillClass.User;
import com.beiming.moudle.auth.service.BussService;
import com.beiming.moudle.auth.service.BussUserService;
import com.beiming.moudle.auth.service.ProductImageService;
import com.beiming.moudle.auth.vo.TokenVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 商家信息
 */
@RestController
@RequestMapping("/buss")
public class BussController {
    private final BussService bussService;
    private final BussUserService bussUserService;

    public BussController(BussService bussinessInfoService, BussUserService bussUserService) {
        this.bussService = bussinessInfoService;
        this.bussUserService = bussUserService;

    }
    @PostMapping("/info")
    public Result getPage(@RequestBody BussPage pa){
        IPage<BussEntity> bussPage = bussService.getPage(pa.getCurrentPage(), pa.getPageSize(),pa.getStatus());
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( pa.getCurrentPage() > bussPage.getPages()){
            bussPage = bussService.getPage((int)bussPage.getPages(), pa.getPageSize(),pa.getStatus());
        }
        List<BussEntity> bussEntityList = bussPage.getRecords();
        for(BussEntity bussEntity : bussEntityList){
            bussEntity.setBussinessAccountName(bussUserService.getBussLoginUser(bussEntity.getBussinessId()));
        }
        return Result.ok(bussPage);
    }
    @PostMapping("/infomsg")
    public Result getMsgPage(@RequestBody BussPage pa){
        IPage<BussEntity> bussPage = bussService.getMsgPage(pa);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( pa.getCurrentPage() > bussPage.getPages()){
            bussPage = bussService.getMsgPage(pa);
        }
        List<BussEntity> bussEntityList = bussPage.getRecords();
        for(BussEntity bussEntity : bussEntityList){
            bussEntity.setBussinessAccountName(bussUserService.getBussLoginUser(bussEntity.getBussinessId()));
        }
        return Result.ok(bussPage);
    }

    @PostMapping("/setStatus")
    private Result setStatus(@RequestBody BussEntity bussEntity){
        int i= bussService.updateBussStatus(bussEntity);
        return Result.ok(i);
    }
    @PostMapping("/setStopStatus")
    private Result setStopStatus(@RequestBody BussEntity bussEntity){
        System.out.println(bussEntity);
        int i= bussService.updateStopBussStatus(bussEntity);
        return Result.ok(i);
    }


    @PostMapping("/login")
    public Result login(@RequestBody User u) {
        System.out.println(u);
        BussUserEntity user = bussUserService.findByUsername(u.getUsername());
        //账号不存在、密码错误
        if (user == null || !user.getPassword().equals(u.getPassword())) {
            return Result.build(400, "用户名或密码错误");
        } else {
            //生成token，并保存到数据库
            String token = bussUserService.createToken(user);
            TokenVO tokenVO = new TokenVO();
            tokenVO.setToken(token);
            tokenVO.setUsername(user.getUserName());
            tokenVO.setId(user.getId());
            return Result.ok(tokenVO);
        }
    }

    /**
     * 登出
     *
     * @param
     * @return
     */
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        //从request中取出token
        String token = TokenUtil.getRequestToken(request).get("bussiness_token").toString();
        System.out.println(token);
        bussUserService.logout(token);
        return Result.ok();
    }


//    @GetMapping("/download/")
//    public Result download(int productId){
//
//        return  Result.ok(productImage);
//    }
}
